package com.qwz.example;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;

/**
* @Description: xml解析工具类
* @Package com.qwz.example.XmlUtil
* @author Qiuwuzhi
* @date 2019/12/27 9:56
* @version V1.0
*/
public class XmlUtil {
    public static Document getRoot(String filename) throws Exception {
        Document document = null;
        //建立一个解析器工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //获取一个DocumentBuilder对象，这个对象代表了具体的dom解析器
        DocumentBuilder builder = factory.newDocumentBuilder();
        //等到一个表示xml文档的Document对象
        document = builder.parse(filename);
        //去掉xml文档中作为格式化内容的空白而映射在dom数上的TextNode对象
        document.normalize();
        return document;
    }
}
